package bcxd.firstline.register.machining;

import java.sql.Connection;
import java.util.Map;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DateTrimmer;
import org.limp.mine.EncryptTool;
import org.limp.mine.StringTool;

import bcxd.firstline.user.manager.AppUserManager;

/**
 * 用户注册数据加工
 * 
 * @author mido
 * 
 */
public class RegisterMachining implements SimpleBeanMachining {

	public SimpleBean machining(SimpleBean bean, Table table,
			CommonBean4HSQ cbh, Connection conn, int type)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (type == -4) {
			String logid = StringTool.checkString(cbh.getResource()
					.get("logid"));

			AppUserManager manager = new AppUserManager(conn);

			if (manager.reduplicateLogid(logid)) {
				throw new MachiningException("reduplicate logid.");
			}

			String utype = StringTool.checkString(cbh.getResource()
					.get("utype"));

			SimpleBean4Table sbean = null;

			if (utype.equals("focalCompany")) {
				sbean = new SimpleBean4TableImpl("BCXD_FCAPPLY");
				sbean.getResource().put("fcname",
						cbh.getResource().get("uname"));
				sbean.getResource().put("fcauditstatus", "0");
				sbean.getResource().put("fcapplydate", DateTrimmer.getYMDHMS());
				sbean.getResource().put("fctype", "focalCompany");
			} else if (utype.equals("creditOrganization")) {
				sbean = new SimpleBean4TableImpl("BCXD_CRAPPLY");
				sbean.getResource().put("cr_name",
						cbh.getResource().get("uname"));
				sbean.getResource().put("cr_status", "0");
				sbean.getResource().put("cr_audit_status", "0");
				sbean.getResource().put("cr_apply_date",
						DateTrimmer.getYMDHMS());
			} else if (utype.equals("personSupplier")) {
				sbean = new SimpleBean4TableImpl("BCXD_SUPAPPLY_PERSONINFO");
				// sbean = new SimpleBean4TableImpl("BCXD_SUPAPPLY");
				sbean.getResource().put("sup_name",
						cbh.getResource().get("uname"));
				sbean.getResource().put("user_id",
						cbh.getResource().get("logid"));
				sbean.getResource().put("sup_audit_status", "0");
				sbean.getResource().put("sup_apply_date",
						DateTrimmer.getYMDHMS());
			} else if (utype.equals("companySupplier")) {
				sbean = new SimpleBean4TableImpl("BCXD_SUCAPPLY");
				sbean.getResource().put("suc_name",
						cbh.getResource().get("uname"));
				sbean.getResource().put("suc_audit_status", "0");
				sbean.getResource().put("suc_apply_date",
						DateTrimmer.getYMDHMS());
				sbean.getResource().put("fctype", "companySupplier");
			}

			if (sbean == null) {
				throw new MachiningException("unknown user type '" + utype
						+ "'.");
			}

			// 创建用户申请信息
			BaseworkUtil basework = new BaseworkUtil();
			String pkid = null;
			try {
				Map map = (Map) basework.manualSave(sbean.getTable(), sbean,
						conn);

				if (sbean.getTable().hasPrimaryKey()) {
					pkid = (String) map
							.get(sbean.getTable().getPrimaryKey()[0]);
				}
			} catch (Exception ex) {
				throw new MachiningException("create user error.", ex);
			}

			// 申请信息主键校验
			if (pkid == null) {
				throw new MachiningException(
						"create user error, primary-key is null.");
			}

			bean.getResource().put(
					"pwd",
					EncryptTool.encryptString((String) bean.getResource().get(
							"pwd")));
			bean.getResource().put("utypeid", pkid);
			bean.getResource().put("ustatus", "0");
			bean.getResource().put("udate", DateTrimmer.getYMDHMS());
		}

		return null;
	}

}
